﻿using DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAO
{
    public class KhuVucDAO
    {
/*-------------------------LOAD----------------------------------------------------
LOADKHUVUC
LOADKHUVUC_MAKV @Ma_KV int
LOADKHUVUC_SOGIONANG @SoGioNang decimal*/

        public DataTable LoadKhuVuc()
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlDataAdapter _da = new SqlDataAdapter("LOADKHUVUC", _cn);
            _da.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;

            DataTable _dt = new DataTable();
            _da.Fill(_dt);
            return _dt;
        }
        public DataTable LoadKhuVuc_MaKV(int Ma_KV)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlDataAdapter _da = new SqlDataAdapter("LOADKHUVUC_MAKV", _cn);
            _da.SelectCommand.CommandType = CommandType.StoredProcedure;
            _da.SelectCommand.Parameters.Add("@Ma_KV", SqlDbType.Int).Value = Ma_KV;

            DataTable _dt = new DataTable();
            _da.Fill(_dt);
            return _dt;
        }
        public DataTable LoadKhuVuc_SoGioNang(float SoGioNang)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlDataAdapter _da = new SqlDataAdapter("LOADKHUVUC_SOGIONANG", _cn);
            _da.SelectCommand.CommandType = CommandType.StoredProcedure;
            _da.SelectCommand.Parameters.Add("@SoGioNang", SqlDbType.Decimal).Value = SoGioNang;

            DataTable _dt = new DataTable();
            _da.Fill(_dt);
            return _dt;
        }


        public int KT_TheoTenKhuVuc(KhuVucDTO kv)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlCommand cmd = new SqlCommand("KT_TheoTenKhuVuc", _cn);
            cmd.CommandType = CommandType.StoredProcedure;


            cmd.Parameters.Add(new SqlParameter("@TenKV", SqlDbType.NVarChar, 50));
            cmd.Parameters.Add(new SqlParameter("@TonTai", SqlDbType.Int));
            cmd.Parameters["@TonTai"].Direction = ParameterDirection.Output;


            cmd.Parameters["@TenKV"].Value = kv.TENKV;
           // cmd.Parameters["@TonTai"].Value = ts.MATS;

            cmd.ExecuteNonQuery();
            int _kq = (int)cmd.Parameters["@TonTai"].Value;
            _cn.Close();
            return _kq;
        }

/*-------------------------INSERT----------------------------------------------------
THEMKHUVUC @TenKV nvarchar(20), @SoGioNang decimal
 */
        public int ThemKhuVuc(KhuVucDTO kv)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlCommand cmd = new SqlCommand("THEMKHUVUC", _cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@TenKV", SqlDbType.NVarChar,20));
            cmd.Parameters.Add(new SqlParameter("@SoGioNang", SqlDbType.Decimal));
            cmd.Parameters["@TenKV"].Value = kv.TENKV;
            cmd.Parameters["@SoGioNang"].Value = kv.SOGIONANG;
            int _kq = cmd.ExecuteNonQuery();
            _cn.Close();
            return _kq;
        }
/*-------------------------UPDATE----------------------------------------------------
SUAKHUVUC @Ma_KV int, @TenKV nvarchar(20), @SoGioNang decimal         
*/
        public int SuaKhuVuc(KhuVucDTO kv)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlCommand cmd = new SqlCommand("SUAKHUVUC", _cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@Ma_KV", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("@TenKV", SqlDbType.NVarChar, 20));
            cmd.Parameters.Add(new SqlParameter("@SoGioNang", SqlDbType.Decimal));
            cmd.Parameters["@Ma_KV"].Value = kv.MA_KV;
            cmd.Parameters["@TenKV"].Value = kv.TENKV;
            cmd.Parameters["@SoGioNang"].Value = kv.SOGIONANG;
            int _kq = cmd.ExecuteNonQuery();
            _cn.Close();
            return _kq;
        }
/*-------------------------DELETE----------------------------------------------------
XOAKHUVUC @Ma_KV int
 */
        public int XoaKhuVuc(int Ma_KV)
        {
            SqlConnection _cn = Provider.ConnectDB();
            SqlCommand cmd = new SqlCommand("XOAKHUVUC", _cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@Ma_KV", SqlDbType.Int));
            cmd.Parameters["@Ma_KV"].Value = Ma_KV;
            int _kq = cmd.ExecuteNonQuery();
            _cn.Close();
            return _kq;
        }
    }
}
